
package com.market.dao;

import java.util.List;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import com.market.bean.Category;
import com.market.dao.hibernate.GenericDaoHibernate;

/**
 * Data access object (DAO) for domain model class Category.
 * 
 * @see com.market.bean.Category
 * @author MyEclipse - Hibernate Tools
 */
public class CategoryDAO extends GenericDaoHibernate<Category, Long>
{

    public CategoryDAO(Class<Category> persistentClass)
    {
        super(persistentClass);
        // TODO Auto-generated constructor stub
    }
    
    public CategoryDAO()
    {
        super();
    }
    
    static final Logger log = LoggerFactory.getLogger(CategoryDAO.class);

    // property constants
    public static final String DESCRIPTION = "description";

    protected void initDao()
    {
        // do nothing
    }

    public Category findById(java.lang.String id)
    {
        log.debug("getting Category instance with id: {}" ,id);
        try
        {
            Category instance = (Category)getHibernateTemplate().get("com.market.bean.Category", id);
            return instance;
        }
        catch (RuntimeException re)
        {
            log.error("get failed", re);
            throw re;
        }
    }

    public List findByExample(Category instance)
    {
        log.debug("finding Category instance by example");
        try
        {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        }
        catch (RuntimeException re)
        {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding Category instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from Category as model where model." + propertyName + "= ?";
            return getHibernateTemplate().find(queryString, value);
        }
        catch (RuntimeException re)
        {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findByDescription(Object description)
    {
        return findByProperty(DESCRIPTION, description);
    }

    public Category merge(Category detachedInstance)
    {
        log.debug("merging Category instance");
        try
        {
            Category result = (Category)getHibernateTemplate().merge(detachedInstance);
            log.debug("merge successful");
            return result;
        }
        catch (RuntimeException re)
        {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Category instance)
    {
        log.debug("attaching dirty Category instance");
        try
        {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        }
        catch (RuntimeException re)
        {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(Category instance)
    {
        log.debug("attaching clean Category instance");
        try
        {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        }
        catch (RuntimeException re)
        {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static CategoryDAO getFromApplicationContext(ApplicationContext ctx)
    {
        return (CategoryDAO)ctx.getBean("CategoryDAO");
    }
}
